//
//  CustomerTableViewController.m
//  HuaRun2
//
//  Created by admin on 17/8/29.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "CustomerTableViewController.h"
#import "TwoLabelView.h"
#import "GiftTableViewCell.h"
#import "ContactTableViewCell.h"
#import "MeetingTableViewCellStyle2.h"
#import "NoteTableViewCell.h"
#import "AddressDetailTableViewCell.h"
#import "RelatePersonTableViewCell.h"
//#import "CompanyModel.h"
#import "NoteModel.h"
#import "AddressModel.h"
#import "ContactPersonModel.h"
#import "Contact_ListModel.h"
#import "Home_Search_NoteCell.h"
#import "Contact_DetailViewController.h"
#import "Com_MeetingModel.h"
#import "Meeting_DetailViewController2.h"

@interface CustomerTableViewController ()

@end

@implementation CustomerTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    if (self.listType == ListTypeMeetiong) {
//        self.title = NSLocalizedString(@"会议",nil);
    }else if (self.listType == ListTypeGift) {
        
    }else if (self.listType == ListTypeAddress) {
        self.title = NSLocalizedString(@"地址详情", nil);
        self.tableView.estimatedRowHeight = 100;
        [self fetchContactListInfo];
    }
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = backItem;

    
    [self.tableView registerClass:[MeetingTableViewCellStyle2 class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[GiftTableViewCell class] forCellReuseIdentifier:@"GiftTableViewCell"];
    [self.tableView registerClass:[AddressDetailTableViewCell class] forCellReuseIdentifier:@"AddressDetailTableViewCell"];
    [self.tableView registerClass:[RelatePersonTableViewCell class] forCellReuseIdentifier:@"RelatePersonTableViewCell"];
    [self.tableView registerClass:[Home_Search_NoteCell class]
           forCellReuseIdentifier:@"Home_Search_NoteCell"];

    self.tableView.backgroundColor = RGBA(249, 249, 249, 1);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    TwoLabelView * headerView = [TwoLabelView new];
    headerView.frame = CGRectMake(kMargin10, 0, kMainScreenWidth - 20, 60);
    headerView.nameLab.text = [NSString showSpaceStringWithStr: _name];
    headerView.companyLab.text = [NSString showSpaceStringWithStr: _companyName];
    self.tableView.tableHeaderView = headerView;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.listType == ListTypeAddress) {
        return 2;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.listType == ListTypeAddress) {
        return 1;
    }

    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.listType == ListTypeGift) {
        GiftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GiftTableViewCell"
                                                                  forIndexPath:indexPath];
        cell.giftModel = _dataArray[indexPath.row];
        return cell;
    }
  
    
    if(self.listType == ListTypeMeetiong) {
        MeetingTableViewCellStyle2 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.com_MeetingModel = _dataArray[indexPath.row];
        return cell;
    
    }
    if (self.listType == ListTypeAddress) {
        if (indexPath.section == 0) {
            AddressDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddressDetailTableViewCell" forIndexPath:indexPath];
            cell.addressModel = _addressModel;
            return cell;
        }

        if (_dataArray.count == 0) {
            Home_Search_NoteCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Home_Search_NoteCell"
                                                                          forIndexPath:indexPath];
            cell.valueLab1.text = NSLocalizedString(@"暂无数据", nil);
            return cell;
        }
        RelatePersonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RelatePersonTableViewCell" forIndexPath:indexPath];
        cell.contactPersonModel = _dataArray[indexPath.row];

        return cell;
    }
    
    MeetingTableViewCellStyle2 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.listType == ListTypeGift) {
        return 150;
    }
    if (self.listType == ListTypeMeetiong) {
        return 115;
    }

    if (self.listType == ListTypeAddress) {
        if (indexPath.section == 0) {
            return UITableViewAutomaticDimension;
        }
        
        if (_dataArray.count == 0) {
            return 44;
        }
        return 100;
    }
    
    return 100;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (self.listType == ListTypeAddress && section == 1) {

        UIView * view = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kMainScreenWidth, 40))];
        view.backgroundColor = RGBA(249, 249, 249, 1);
        
        UILabel * label = [[UILabel alloc] init];
        label.frame = CGRectMake(10, 10, 150, 20);
        label.font = kFontSize15;
        label.text = NSLocalizedString(@"相关联系人", nil);
        label.textColor = [UIColor colorWithHex:@"3c3c3c"];
        [view addSubview:label];
        
        return view;
    }
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (self.listType == ListTypeAddress && section == 1) {
        return 40;
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.listType == ListTypeMeetiong) {
        Meeting_DetailViewController2 * detailVC = [Meeting_DetailViewController2 new];
        Com_MeetingModel * meetingModel = self.dataArray[indexPath.row];
        detailVC.meetingId = meetingModel.id;
        detailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
    
    if (self.listType == ListTypeAddress && indexPath.section == 1) {
        Contact_DetailViewController * meetingVC = [[Contact_DetailViewController alloc] init];
        meetingVC.hidesBottomBarWhenPushed = YES;
        ContactPersonModel * personModel = _dataArray[indexPath.row];
        meetingVC.contactId = personModel.id;
        [self.navigationController pushViewController:meetingVC animated:YES];
    }
    
}
#pragma mark - 地址详情相关时候
//获取相关联系人
- (void)fetchContactListInfo {
    NSString * url = [NSString stringWithFormat:@"%@%@", kBaseUrl,
                      kCompanyDetail_ContactListUrl];
    NSString * accountId = self.addressModel ? (self.addressModel.id ?
                                                self.addressModel.id : @"" )
                                            : @"";
    NSDictionary * dic = @{@"addressId": accountId};
    
    weakself(ws);
    [NetWorkHelper getWithURLString:url parameters:dic success:^(id object) {
        if (![object[@"code"] isEqual: kSuccessCode]) {
            NSString * info = [NSString isemptyStr:object[@"msg"]] ? kUnknownError : object[@"msg"];
            [ws showWithText:info];
            return;
        }
        ws.dataArray = [NSArray yy_modelArrayWithClass:[ContactPersonModel class] json:object[@"list"]];
        [ws.tableView reloadData];
        
    } failure:^(NSError *error) {
        [ws showWithText:kNetworkError];
    }];
    
}


@end
